# -*- coding: utf-8 -*-
# @Time : 2022/6/22 22:14
# @Author : cheney
# @File : infraredController.py
# @Software: PyCharm
# @Site: www.cheney.cc
"""
 红外避障模块
 todo:功能比较简单，获取两侧的距离（是否小于规定的距离）即可
"""
import RPi.GPIO as GPIO
import time
import sys


class infraredController:
    SensorRight = 16
    SensorLeft = 12

    BtnPin = 19
    Gpin = 5
    Rpin = 6

    def __init__(self):
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)  # Numbers GPIOs by physical location
        GPIO.setup(self.Gpin, GPIO.OUT)  # Set Green Led Pin mode to output
        GPIO.setup(self.Rpin, GPIO.OUT)  # Set Red Led Pin mode to output
        GPIO.setup(self.BtnPin, GPIO.IN,
                   pull_up_down=GPIO.PUD_UP)  # Set BtnPin's mode is input, and pull up to high level(3.3V)
        GPIO.setup(self.SensorRight, GPIO.IN)
        GPIO.setup(self.SensorLeft, GPIO.IN)
        self.keysacn()

    def keysacn(self):
        # # todo:这是干嘛的 ？
        # val = GPIO.input(self.BtnPin)
        # while not GPIO.input(self.BtnPin):
        #     val = GPIO.input(self.BtnPin)
        # while GPIO.input(self.BtnPin):
        #     time.sleep(0.01)
        #     val = GPIO.input(self.BtnPin)
        #     if val:
        #         GPIO.output(self.Rpin, 1)
        #         while not GPIO.input(self.BtnPin):
        #             GPIO.output(self.Rpin, 0)
        #     else:
        #         GPIO.output(self.Rpin, 0)
        pass

    def right_closer(self) -> bool:
        """
        右边是否过近
        :return:
        """
        return not GPIO.input(self.SensorRight)

    def left_closer(self) -> bool:
        """
        左边是否过近
        :return:
        """
        return not GPIO.input(self.SensorLeft)
